package com.qfedu.lc.user.controller;


import com.qfedu.lc.common.config.RedisKeyConfig;
import com.qfedu.lc.common.param.PayAdd;
import com.qfedu.lc.common.vo.R;
import com.qfedu.lc.user.entity.User;
import com.qfedu.lc.user.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 6.用户钱包表 前端控制器
 * </p>
 *
 * @author wgz
 * @since 2024-01-29
 */
@RestController
@RequestMapping("/user/wallet")
public class WalletController {

    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    private WalletService walletService;

    // user/wallet/create

    @PostMapping("/create")
    public R  create(@RequestBody PayAdd payAdd, HttpServletRequest httpServletRequest){

        // 1.检查参数
        if(payAdd == null){
            return R.fail("充值失败");
        }


        // 获取用户id
        String token = httpServletRequest.getHeader("Lctoken");
        User user = (User) redisTemplate.opsForValue().get(RedisKeyConfig.LOGIN_TOKEN+token);
        payAdd.setUserId(user.getId());

        // 2.根据支付类型不同 进行支付
        switch (payAdd.getPayType()){

            case 2:// 支付宝支付
                // 记录支付的日志  发起支付宝支付
               return   walletService.rechargeByZhiFuBao(payAdd);
            case 3:// 微信支付

               break;
        }

        return R.fail("充值失败");

    }

}

